package com.ruoyi.charge.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.ruoyi.charge.domain.Charger;
import com.ruoyi.charge.domain.ChargerGun;
import com.ruoyi.charge.mapper.ChargerGunMapper;
import com.ruoyi.charge.service.IChargerGunService;
import com.ruoyi.charge.service.IChargerService;
import com.ruoyi.common.constants.CacheConstant;
import com.ruoyi.common.enums.EYn;
import com.ruoyi.common.enums.GunStatusEnum;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.common.web.exception.ServiceException;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Transaction;

import java.util.List;

/**
 * 充电枪 服务层实现
 *
 * @author laifeng
 */
@Component
public class ChargerGunServiceImpl extends ServiceImpl<ChargerGunMapper, ChargerGun> implements IChargerGunService {

    @Inject
    ChargerGunMapper chargerGunMapper;
    @Inject
    RedisUtil redisUtil;
    @Inject
    IChargerService chargerService;

    @Override
    public List<ChargerGun> getChargerGunListCacheBySn(String sn) {
        List<ChargerGun> chargerGunList = redisUtil.getCacheList(CacheConstant.getChargerGunListBySn(sn));
        if (CollUtil.isEmpty(chargerGunList)) {
            chargerGunList = this.list(new QueryWrapper().eq(ChargerGun::getChargerSn, sn));
            if (ArrayUtil.isNotEmpty(chargerGunList)) {
                redisUtil.setCacheList(CacheConstant.getChargerGunListBySn(sn), chargerGunList);
            }
        }
        return chargerGunList;
    }

    @Override
    public ChargerGun getChargerGunCacheBySnAndNumber(String sn, String number) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(ChargerGun::getChargerSn, sn)
                .eq(ChargerGun::getNumber, number);
        return chargerGunMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    @Transaction
    public boolean addChargerGun(ChargerGun chargerGun) {
        if (chargerGun.getChargerId() == null) {
            throw new ServiceException("充电桩id不能为空");
        }
        Charger charger = chargerService.getById(chargerGun.getChargerId());
        if (charger == null) {
            throw new ServiceException("充电桩不存在");
        }
        chargerGun.setChargerSn(charger.getSn());
        delCacheToChargerGunList(chargerGun.getChargerSn());
        // 判断充电枪码是否已存在
        boolean exists = this.exists(new QueryWrapper().eq(ChargerGun::getChargerSn, chargerGun.getChargerSn()).eq(ChargerGun::getNumber, chargerGun.getNumber()));
        if (exists) {
            throw new ServiceException("枪号已存在");
        }
        boolean result = this.save(chargerGun);
        if (result) {
            charger.setGunNum(charger.getGunNum() + 1);
            chargerService.updateById(charger);
        }
        return result;
    }

    @Override
    public boolean updateByIdChargerGun(ChargerGun chargerGun) {
        delCacheToChargerGunList(chargerGun.getChargerSn());
        return this.updateById(chargerGun);
    }

    @Override
    public boolean removeChargerGunsByIds(List<Long> list) {
        List<ChargerGun> chargerGunList = this.listByIds(list);
        if (ArrayUtil.isNotEmpty(chargerGunList)) {
            for (ChargerGun chargerGun : chargerGunList) {
                delCacheToChargerGunList(chargerGun.getChargerSn());
            }
        }
        Charger charger = chargerService.getById(chargerGunList.get(0).getChargerId());
        charger.setGunNum(charger.getGunNum() - chargerGunList.size());
        boolean result = this.removeByIds(list);
        if (result) {
            chargerService.updateById(charger);
        }
        return result;
    }

    @Override
    public void setChargerGunOfflineByChargerSn(String chargerSn) {
        this.updateChain().set(ChargerGun::getGunStatus, GunStatusEnum.OFFLINE.getCode())
                .set(ChargerGun::getIsInsert, EYn.NO.getCode())
                .eq(ChargerGun::getChargerSn, chargerSn)
                .update();
    }

    private void delCacheToChargerGunList(String sn) {
        redisUtil.deleteCacheList(CacheConstant.getChargerGunListBySn(sn));
    }
}
